package com.base;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;

import androidx.activity.result.ActivityResultLauncher;
import androidx.core.content.ContextCompat;

// PermissionHelper.java
public class PermissionHelper {
    private final Activity activity;
    
    public PermissionHelper(Activity activity) {
        this.activity = activity;
    }
    
    /**
     * 检查所有必需权限是否已授予
     */
    public boolean checkAllPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            return checkAndroid14Permissions();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            return checkAndroid11To13Permissions();
        } else {
            return checkLegacyPermissions();
        }
    }
    
    /**
     * Android 14+ 权限检查
     */
    private boolean checkAndroid14Permissions() {
        int imagePermission = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES);
        int videoPermission = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_VIDEO);
        int audioPermission = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_AUDIO);
        
        return imagePermission == PackageManager.PERMISSION_GRANTED &&
               videoPermission == PackageManager.PERMISSION_GRANTED &&
               audioPermission == PackageManager.PERMISSION_GRANTED;
    }
    
    /**
     * Android 11-13 权限检查
     */
    private boolean checkAndroid11To13Permissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            return Environment.isExternalStorageManager();
        }
        return false;
    }
    
    /**
     * Android 10及以下版本权限检查
     */
    private boolean checkLegacyPermissions() {
        int writePermission = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int readPermission = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
        
        return writePermission == PackageManager.PERMISSION_GRANTED && 
               readPermission == PackageManager.PERMISSION_GRANTED;
    }
    
    /**
     * 请求必要权限
     */
    public void requestPermissions(ActivityResultLauncher<String[]> launcher) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            requestAndroid14Permissions(launcher);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            requestAndroid11To13Permissions();
        } else {
            requestLegacyPermissions(launcher);
        }
    }
    
    private void requestAndroid14Permissions(ActivityResultLauncher<String[]> launcher) {
        String[] permissions = {
            Manifest.permission.READ_MEDIA_IMAGES,
            Manifest.permission.READ_MEDIA_VIDEO,
            Manifest.permission.READ_MEDIA_AUDIO
        };
        launcher.launch(permissions);
    }
    
    private void requestAndroid11To13Permissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                // 权限已授予，无需操作
            } else {
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                intent.setData(uri);
                activity.startActivity(intent);
            }
        }
    }
    
    private void requestLegacyPermissions(ActivityResultLauncher<String[]> launcher) {
        String[] permissions = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
        };
        launcher.launch(permissions);
    }
}
